#region License Revision: 0 Last Revised: 6/18/2008 11:00 AM
/******************************************************************************
Copyright (c) Microsoft Corporation.  All rights reserved.


This file is licensed under the Microsoft Public License (Ms-PL). A copy of the Ms-PL should accompany this file. 
If it does not, you can obtain a copy from: 

http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
******************************************************************************/
#endregion // License
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.Windows.Markup;
using ScriptInterop;

namespace SilverlightPopup
{
	public class DataBind
	{
		private string name;
		private string data;
		private string desc;
		private string image;

		public DataBind() { }

		public string Name
		{
			get { return name; }
			set { name = value; }
		}

		public string Data
		{
			get { return data; }
			set { data = value; }
		}

		public string Image
		{
			get { return image; }
			set { image = value; }
		}

		public string Description
		{
			get { return desc; }
			set { desc = value; }
		}
	}

	[ScriptableType]
	public partial class Page : UserControl
	{
		private bool computeSize = false;
		private Popup popup;
		public Page()
		{
			InitializeComponent();
			this.SizeChanged += new SizeChangedEventHandler(Popup_SizeChanged);
			this.LayoutUpdated += new EventHandler(Popup_LayoutUpdated);
			// for testing hide on click
			//this.MouseLeftButtonDown += new MouseButtonEventHandler(Page_MouseLeftButtonDown);
			this.MouseLeftButtonUp += new MouseButtonEventHandler(Popup_MouseLeftButtonUp);
			this.MouseLeave += new MouseEventHandler(Popup_MouseLeave);
			popup = new Popup(this);
			HtmlPage.RegisterScriptableObject("popup", popup);


			//DataBind db = new DataBind();
			//db.Name = "Sensor Example";
			//db.Data = "23 degrees F";
			//db.Description = "Temperature sensor";
			//this.Popup.DataContext = db;
		}

		void Page_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			//popup.HidePopup();
		}

		void Popup_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			//popup.HidePopup();
		}

		void Popup_MouseLeave(object sender, MouseEventArgs e)
		{
			popup.HidePopup();
		}

		// property to hold the compute size flag
		public bool ComputeSize
		{
			get { return computeSize; }
			set { computeSize = value; }
		}

		/// <summary>
		/// When the layout is updated we ant to resize the control to fit the content. So we resize the silverlight plugin, which will trigger the SizeChanged event to take care of the HTML elements.
		/// </summary>
		/// <param name="sender">sender of event</param>
		/// <param name="e">event arguments</param>
		void Popup_LayoutUpdated(object sender, EventArgs e)
		{
			object obj = HtmlPage.Plugin.GetProperty("Width");
			//if (computeSize)
			{
				HtmlPage.Plugin.SetProperty("Height", this.ActualHeight);
				HtmlPage.Plugin.SetProperty("Width", this.ActualWidth);
				computeSize = false;
			}
		}

		/// <summary>
		/// if popup size change recompute the div and all other HML elements inside the div size
		/// </summary>
		/// <param name="sender">sender of event</param>
		/// <param name="e">event arguments</param>
		void Popup_SizeChanged(object sender, SizeChangedEventArgs e)
		{
			double width = this.ActualWidth;
			double height = this.ActualHeight;
			ResizePopup(width, height);
		}

		/// <summary>
		/// Resize the popup. Resizes all the HTML elements that surround the silverlight control, so that everything is taken care of.
		/// </summary>
		/// <param name="width">width of the popup</param>
		/// <param name="height">height of the popup</param>
		public static void ResizePopup(double width, double height)
		{
			HtmlElement elem = HtmlPage.Plugin;
			while (elem != null)
			{
				elem.SetStyleAttribute("height", height.ToString());
				elem.SetStyleAttribute("width", width.ToString());
				// keep going until you have found the div that contains the control
				if (elem.TagName == "div")
					break;
				elem = elem.Parent;
			}
		}
	}
}
